/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IA;

import iterators.ShortestWorkloadServerItr;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Jeremy
 */
public class ServerCollection {

    private ArrayList<Server> _servers;

    public ServerCollection(int NumServers, Observable Kitchen) {
        _servers = new ArrayList<Server>();
        for (int i = 0; i < NumServers; i++) {
            _servers.add(new Server(_servers.size(), Kitchen));
        }
    }

    public ServerCollection() {
        _servers = new ArrayList<Server>();
    }
    
    public Iterator<Server> getShortestWorkloadServerItr() {
        return new ShortestWorkloadServerItr(_servers);
    }

    public Iterator<Server> getServerIterator() {
        return _servers.iterator();
    }
    
    public Server getServer(int ID) {
        return _servers.get(ID);
    }

    public int getNumServers() {
        return _servers.size();
    }

    public void addServer(Observable Kitchen) {
        _servers.add(new Server(_servers.size(), Kitchen));
        
    }

    public void removeServer(int i, Observable Kitchen) {
        if ((i >= 0) & (i <= _servers.size())) {
            Kitchen.RemoveObserver(_servers.get(i));
            _servers.remove(i);
        }
    }

    public void setNumServers(int numOfServers, Observable Kitchen) {
        int diff = numOfServers - _servers.size();
        if (diff > 0) {
            for (int i = 0; i < diff; i++) {
                addServer(Kitchen);
            }
        } else {
            if (diff < 0) {
                for (int i = 0; i < (Math.abs(diff)); i++) {
                    removeServer(i, Kitchen);
                }
            }
        }
    }
}
